package com.usian.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.usian.config.JdbcConfig;
import com.usian.pojo.User;
import com.usian.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {

    private static  final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private JdbcConfig jdbcConfig;

    @RequestMapping("/user")
    public String User(){
        return "我的第一个springboot项目";
    }


    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("msg","您好");
        return "index.html";
    }

    @RequestMapping("/emp/{id}/{name}")
    public String emp(@PathVariable("id")String id, @PathVariable("name")String name, Model model){
        model.addAttribute("id",id);
        model.addAttribute("name",name);
        return "index";
    }

    @RequestMapping("index1")
    public String index1(){
        String name =null;
        name.length();
        return "index";
    }

    @RequestMapping("testJunit")
    @ResponseBody
    public String testJunit(){
        return "我要测试一下junit";
    }


    /**
     * 查询所有用户信息
     *
     * @param model
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<User> userList = userService.findAll();
        model.addAttribute("userList",userList);
        return "index";
    }

    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "add";
    }


    /**
     * 添加/修改用户
     *
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(User user){
        if(user.getUserId() ==null){
            userService.addUser(user);
        }else{
            userService.updateUser(user);
        }
        return "redirect:/users/findAll";
    }

    /**
     * 修改回显
     *
     * @param userId
     * @return
     */
    @RequestMapping("/findById")
    public String findById(Integer userId,Model model){
        User user = userService.findById(userId);
        model.addAttribute("users",user);
        return "update";
    }

    /**
     * 删除
     * @param userId
     * @return
     */
    @RequestMapping("/deleById")
    public String deleById(Integer userId){
        userService.deleteById(userId);
        return "redirect:/users/findAll";
    }



}
